package com.xb.cloud.usercenter.client;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.usercenter.client.vo.OrderQueryVo;
import com.xb.cloud.usercenter.service.SellOrderApiService;

/**
 * 
 * @ClassName: SellOrderAdapter
 * @Description:用户订单操作
 * @author: 鲜备科技
 * @date: 2018年1月4日 下午6:47:36
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class SellOrderAdapter extends Adapter {

	private final Logger logger = LoggerFactory.getLogger(SellOrderAdapter.class);

	@Resource
	SellOrderApiService sellOrderApiService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		long time = System.currentTimeMillis();

		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		long currentUserId = context.getCurrentUserId();

		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("companyId", companyId);
		paramMap.put("currentUserId", currentUserId);

		if ("/h5/usercenter/order/list".equalsIgnoreCase(urlPath)) {
			// 用户订单列表，分不同类型（订单状态），分页
			paramMap.putAll(bizInfo);
			OrderQueryVo queryVo = BeanUtils.convertMapToBean(OrderQueryVo.class, paramMap);
			return sellOrderApiService.getOrderListByStatus(queryVo);
		}

		if ("/h5/usercenter/order/count".equalsIgnoreCase(urlPath)) {
			// 用户订单计数
			String status = bizInfo.get("status");
			OrderQueryVo queryVo = new OrderQueryVo();
			queryVo.setCompanyId(companyId);
			queryVo.setCurrentUserId(currentUserId);
			queryVo.setStatus(status);
			return sellOrderApiService.getOrderCountWithStatus(queryVo);
		}
		
		if ("/h5/usercenter/order/detail".equalsIgnoreCase(urlPath)) {
			// 用户订单计数
			String orderId = bizInfo.get("orderId");
			OrderQueryVo queryVo = new OrderQueryVo();
			queryVo.setCompanyId(companyId);
			queryVo.setCurrentUserId(currentUserId);
			queryVo.setOrderId(Long.valueOf(orderId));
			return sellOrderApiService.getOrderDetailsById(queryVo);
		}

		if ("/h5/usercenter/order/notifyMerchant".equalsIgnoreCase(urlPath)) {
			// 用户订单计数
			Long orderId = Long.valueOf(bizInfo.get("orderId"));
			return sellOrderApiService.notifyMerchantOrderPayed(orderId);
		}
		
		logger.debug("执行时间：{}", System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/usercenter/order/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}
}
